package com.zhuge_new.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.makeramen.roundedimageview.RoundedImageView;
import com.zhuge_new.Adapter.Redexanmine2Adapter;
import com.zhuge_new.Base.BaseFragment;
import com.zhuge_new.R;
import com.zhuge_new.bean.RedexanminBean;
import com.zhuge_new.bean.Redexanmine2Bean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;


/**
 * Created by lenovo on 2018-09-27.
 */

public class RedexanmineFragmen2 extends BaseFragment {

    private RecyclerView redexan2_list;
    private View head2;
    private RoundedImageView redxanmine_logo2;
    private TextView redxanmin_money2;
    private TextView red_num2;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_redexanmine2,container,false);
        head2 = inflater.inflate(R.layout.redxanmine_head,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init_title();
        init_gata();
    }

    @Override
    public void init_title() {
        super.init_title();
        redexan2_list = getActivity().findViewById(R.id.redexan2_list);
        redexan2_list.setLayoutManager(new LinearLayoutManager(getActivity()));

        red_num2 = head2.findViewById(R.id.red_num);
        redxanmine_logo2 = head2.findViewById(R.id.redxanmine_logo);
        redxanmin_money2 = head2.findViewById(R.id.redxanmin_money);

        SpannableStringBuilder builder=new SpannableStringBuilder(red_num2.getText());
        ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.red_red));
        builder.setSpan(foregroundColorSpan,2,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    private void init_gata() {
        OkGo.<String>post(BaseHttp.selSendRedpacketList)
                .tag(this)
                .params("OpenId", PreferencesUtils.getString(getActivity(),"unionid"))
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        Redexanmine2Bean redexanmine2Bean= (Redexanmine2Bean) new DataParse().getBean(response.body(),Redexanmine2Bean.class,0);
                        Redexanmine2Adapter redexanmine2Adapter=new Redexanmine2Adapter(getActivity(),redexanmine2Bean);
                        redexanmine2Adapter.addHeaderView(head2);
                        redexan2_list.setAdapter(redexanmine2Adapter);

                        double month_num=0;
                        for (int i = 0; i< redexanmine2Bean.getObject().size(); i++){
                            month_num= Float.parseFloat(redexanmine2Bean.getObject().get(i).getMoney())+month_num;
                        }
                        redxanmin_money2.setText(String.valueOf(NumberUtil.saveOneBitTwoRound(month_num)));
                        red_num2.setText("发出"+redexanmine2Bean.getObject().size()+"个红包，共计");
                    }
                });
    }
}
